package org.dromara.bzIns.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.bzIns.domain.vo.UserCarVo;
import org.dromara.bzIns.domain.bo.UserCarBo;
import org.dromara.bzIns.service.IUserCarService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 用户汽车
 *
 * @author bz_huaYal9146
 * @date 2025-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/bzIns/userCar")
public class UserCarController extends BaseController {

    private final IUserCarService userCarService;

    /**
     * 查询用户汽车列表
     */
    @SaCheckPermission("bzIns:userCar:list")
    @GetMapping("/list")
    public TableDataInfo<UserCarVo> list(UserCarBo bo, PageQuery pageQuery) {
        return userCarService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户汽车列表
     */
    @SaCheckPermission("bzIns:userCar:export")
    @Log(title = "用户汽车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserCarBo bo, HttpServletResponse response) {
        List<UserCarVo> list = userCarService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户汽车", UserCarVo.class, response);
    }

    /**
     * 获取用户汽车详细信息
     *
     * @param carId 主键
     */
    @SaCheckPermission("bzIns:userCar:query")
    @GetMapping("/{carId}")
    public R<UserCarVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long carId) {
        return R.ok(userCarService.queryById(carId));
    }

    /**
     * 新增用户汽车
     */
    @SaCheckPermission("bzIns:userCar:add")
    @Log(title = "用户汽车", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserCarBo bo) {
        return toAjax(userCarService.insertByBo(bo));
    }

    /**
     * 修改用户汽车
     */
    @SaCheckPermission("bzIns:userCar:edit")
    @Log(title = "用户汽车", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserCarBo bo) {
        return toAjax(userCarService.updateByBo(bo));
    }

    /**
     * 删除用户汽车
     *
     * @param carIds 主键串
     */
    @SaCheckPermission("bzIns:userCar:remove")
    @Log(title = "用户汽车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{carIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] carIds) {
        return toAjax(userCarService.deleteWithValidByIds(List.of(carIds), true));
    }
}
